use ggez::{glam::Vec2, Context, graphics, mint::Point2,};



pub struct SlidingBox {
    x_location: f32,
    origin: Vec2,
    angle: f32,
    x_velocity: f32,
    x_acceleration: f32
}

impl SlidingBox {
    pub fn new() -> SlidingBox {
        let origin = Vec2 { x: 400.0, y: 300.0 };
        let x_location = 0.0;
        let angle = 0.0;
        let x_velocity = 0.0;
        let x_acceleration = 0.0;
        SlidingBox { x_location, origin, angle, x_velocity, x_acceleration }
    }

    pub fn update(&mut self, ctx: &mut Context) -> () {
        self.angle = -(Vec2{x: ctx.mouse.position().x, y: ctx.mouse.position().y} - self.origin).angle_between(Vec2 { x: 1.0, y:  0.0});
        let gravity = 0.4;
        let f_normal = gravity*self.angle.sin();
        self.x_acceleration = f_normal;
        self.x_velocity += self.x_acceleration;
        self.x_location +=self.x_velocity;
    }

    pub fn display(&self, ctx: &mut Context, canvas : &mut graphics::Canvas) -> () {
        let param = graphics::DrawParam::default().dest(self.origin).rotation(self.angle);
        let line = graphics::Mesh::new_line(
            ctx, 
            &[Point2{x: -500.0, y: 0.0}, Point2{x: 500.0, y: 0.0}], 
            2.0, 
            graphics::Color::BLACK
        ).unwrap();
        canvas.draw(&line, param);
        let rect = graphics::Mesh::new_rectangle(
            ctx, 
            graphics::DrawMode::fill(), 
            graphics::Rect { x: self.x_location, y: -20.0, w: 20.0, h: 20.0 }, 
            graphics::Color::new(0.7, 0.7, 0.7, 1.0)).unwrap();
        canvas.draw(&rect, param);

        let rect = graphics::Mesh::new_rectangle(
            ctx, 
            graphics::DrawMode::stroke(2.0), 
            graphics::Rect { x: self.x_location, y: -20.0, w: 20.0, h: 20.0 }, 
            graphics::Color::BLACK).unwrap();
        canvas.draw(&rect, param);
    }
}